import { Node, Prefab, instantiate } from "cc";
import { EntityType } from "../vo/GameConst";

export class EntityPool {
    private static Pool:{[type:number]:Node[]} = {};
    public static getEntityObj(type:EntityType,prefab:Prefab){
        let poolList = EntityPool.Pool[type];
        if(!poolList){
            poolList = [];
            EntityPool.Pool[type] = poolList;
        }
        let node = poolList.pop();
        if(!node && prefab){
            node = instantiate(prefab);
        }
        return node;
    }   

    public static recycleEntityObj(type:EntityType,node:Node){
        node.removeFromParent();
        node.parent = null;
        node.active = false;
        node.setPosition(0,0,0);
        let poolList = EntityPool.Pool[type];
        if(!poolList){
            poolList = [];
            EntityPool.Pool[type] = poolList;
        }
        poolList.push(node);
    }
}